<?php

namespace App\Formats;

use App\Common\Libs\ArrayHelper;
use App\Common\Libs\IdBuilder;
use App\Common\Libs\Json;
use App\Consts\GlobalConst;
use App\Models\Cache\CacheKey;
use App\Models\Field\ArticleField;
use App\Models\Field\NoteField;
use App\Models\Field\UserField;
use App\Models\Select\ArticleSelect;
use App\Models\Select\UserSelect;
use App\Models\SysNote;

class NoteFormat extends BaseFormat
{
    public const SQUARE_RECOMMEND_LIST = 'squareRecommendList';

    public const SEARCH_LIST = 'searchList';

    public const HOME_COLLECT_LIST = 'homeCollectList';

    public const TOPIC_LIST = 'topicList';

    public function formatNoteList($noteIds, $currentUserId, $formatScene)
    {
        switch ($formatScene) {
            case self::HOME_COLLECT_LIST:
            case self::SELF_LIST:
                return self::formatBySelfList($noteIds, $currentUserId);
            case self::TOPIC_LIST:
                return self::formatByTopic($noteIds, $currentUserId);
            case self::SQUARE_RECOMMEND_LIST:
                return self::formatBySquareRecommendList($noteIds, $currentUserId);
            case self::SEARCH_LIST:
                return self::formatBySearchList($noteIds, $currentUserId);
        }

        return [];
    }

    public static function formatBySelfList($noteIds, $currentUserId)
    {
        $noteIdTable = IdBuilder::getSubTable($noteIds);

        $noteArr = self::getNoteArr($noteIdTable);

        $noteContentArr = self::getNoteContentArr($noteIdTable);

        $noteIndexArr = self::getNoteIndexeArr($noteIdTable);

        $noteSelectedArr = self::getNoteSelectedArr($noteIdTable);

        $likeArr = LikeFormat::getLikeArrByTargetIds($currentUserId, $noteIds);

        $collectArr = CollectFormat::getCollectArrByTargetIds($currentUserId, $noteIds);

        $articleArr = ArticleFormat::getArticleArr(ArrayHelper::getColumn($noteArr, 'article_id'), ArticleSelect::NOTE);

        $userArr = UserFormat::getUserArr(ArrayHelper::getColumn($noteArr, 'user_id'), UserSelect::NOTE);

        $topicArr = TopicFormat::getTargetTopicArr(ArrayHelper::getColumn($noteArr, 'note_id'));

        $imageArr = self::getNoteImageArr($noteIdTable);

        $list = [];
        /** @var SysNote $note */
        foreach ($noteArr as $noteId => $note) {
            $list[] = ArrayHelper::merge($note->commonFormat(NoteField::SELF_LIST), [
                'start_index' => isset($noteIndexArr[$note->note_id]) ? $noteIndexArr[$note->note_id]->start_index : 0,
                'end_index' => isset($noteIndexArr[$note->note_id]) ? $noteIndexArr[$note->note_id]->end_index : 0,
                'content' => isset($noteContentArr[$note->note_id]) ? $noteContentArr[$note->note_id]->content : '',
                'selected' => isset($noteSelectedArr[$note->note_id]) ? $noteSelectedArr[$note->note_id]->selected : '',
                'is_like' => isset($likeArr[$note->note_id]) && $likeArr[$note->note_id]->is_deleted == GlobalConst::IS_NOT_DELETE,
                'is_collect' => isset($collectArr[$note->note_id]) && $collectArr[$note->note_id]->is_deleted == GlobalConst::IS_NOT_DELETE,
                'user' => isset($userArr[$note->user_id]) ? $userArr[$note->user_id]->commonFormat(UserField::NOTE) : (object)[],
                'article' => isset($articleArr[$note->article_id]) ? $articleArr[$note->article_id]->commonFormat(ArticleField::NOTE) : (object)[],
                'topic' => isset($topicArr[$note->note_id]) ? $topicArr[$note->note_id] : [],
                'image' => isset($imageArr[$note->note_id]) ? ArrayHelper::getColumn(Json::decode($imageArr[$note->note_id]->url), 'url') : [],
                'count' => service()->CounterService->getTargetCount(CacheKey::COUNTER_NOTE, [
                    'comment',
                    'like',
                    'collect'
                ], ['note_id' => $note->note_id]),
            ]);
        }

        return $list;
    }

    public static function formatByTopic($noteIds, $currentUserId)
    {
        $noteIdTable = IdBuilder::getSubTable($noteIds);

        $noteArr = self::getNoteArr($noteIdTable);

        $noteContentArr = self::getNoteContentArr($noteIdTable);

        $noteIndexArr = self::getNoteIndexeArr($noteIdTable);

        $noteSelectedArr = self::getNoteSelectedArr($noteIdTable);

        $likeArr = LikeFormat::getLikeArrByTargetIds($currentUserId, $noteIds);

        $collectArr = CollectFormat::getCollectArrByTargetIds($currentUserId, $noteIds);

        $articleArr = ArticleFormat::getArticleArr(ArrayHelper::getColumn($noteArr, 'article_id'), ArticleSelect::NOTE);

        
        $topicArr = TopicFormat::getTargetTopicArr(ArrayHelper::getColumn($noteArr, 'note_id'));
        $userArr = UserFormat::getUserArr(ArrayHelper::getColumn($noteArr, 'user_id'), UserSelect::NOTE);

        $imageArr = self::getNoteImageArr($noteIdTable);

        $list = [];
        /** @var SysNote $note */
        foreach ($noteArr as $noteId => $note) {
            $list[] = ArrayHelper::merge($note->commonFormat(NoteField::SELF_LIST), [
                'start_index' => isset($noteIndexArr[$note->note_id]) ? $noteIndexArr[$note->note_id]->start_index : 0,
                'end_index' => isset($noteIndexArr[$note->note_id]) ? $noteIndexArr[$note->note_id]->end_index : 0,
                'content' => isset($noteContentArr[$note->note_id]) ? $noteContentArr[$note->note_id]->content : '',
                'selected' => isset($noteSelectedArr[$note->note_id]) ? $noteSelectedArr[$note->note_id]->selected : '',
                'is_like' => isset($likeArr[$note->note_id]) && $likeArr[$note->note_id]->is_deleted == GlobalConst::IS_NOT_DELETE,
                'is_collect' => isset($collectArr[$note->note_id]) && $collectArr[$note->note_id]->is_deleted == GlobalConst::IS_NOT_DELETE,
                'user' => isset($userArr[$note->user_id]) ? $userArr[$note->user_id]->commonFormat(UserField::NOTE) : (object)[],
                'article' => isset($articleArr[$note->article_id]) ? $articleArr[$note->article_id]->commonFormat(ArticleField::NOTE) : (object)[],
                'topic' => isset($topicArr[$note->note_id]) ? $topicArr[$note->note_id] : [],
                'image' => isset($imageArr[$note->note_id]) ? ArrayHelper::getColumn(Json::decode($imageArr[$note->note_id]->url), 'url') : [],
                'count' => service()->CounterService->getTargetCount(CacheKey::COUNTER_NOTE, [
                    'comment',
                    'like',
                    'collect'
                ], ['note_id' => $note->note_id]),
            ]);
        }

        return $list;
    }

    public static function formatBySquareRecommendList($noteIds, $currentUserId)
    {
        $noteIdTable = IdBuilder::getSubTable($noteIds);

        $noteArr = self::getNoteArr($noteIdTable);

        $noteContentArr = self::getNoteContentArr($noteIdTable);

        $noteIndexArr = self::getNoteIndexeArr($noteIdTable);

        $noteSelectedArr = self::getNoteSelectedArr($noteIdTable);

        $likeArr = LikeFormat::getLikeArrByTargetIds($currentUserId, $noteIds);

        $collectArr = CollectFormat::getCollectArrByTargetIds($currentUserId, $noteIds);

        $articleArr = ArticleFormat::getArticleArr(ArrayHelper::getColumn($noteArr, 'article_id'), ArticleSelect::NOTE);

        $userArr = UserFormat::getUserArr(ArrayHelper::getColumn($noteArr, 'user_id'), UserSelect::NOTE);

        $imageArr = self::getNoteImageArr($noteIdTable);

        $relationship = service()->RelationshipService->getRelationshipByIdArr($currentUserId, ArrayHelper::getColumn($noteArr, 'user_id'));

        $list = [];
        /** @var SysNote $note */
        foreach ($noteArr as $noteId => $note) {
            $list[] = ArrayHelper::merge($note->commonFormat(NoteField::SELF_LIST), [
                'target_type' => GlobalConst::TARGET_TYPE_NOTE,
                'start_index' => isset($noteIndexArr[$note->note_id]) ? $noteIndexArr[$note->note_id]->start_index : 0,
                'end_index' => isset($noteIndexArr[$note->note_id]) ? $noteIndexArr[$note->note_id]->end_index : 0,
                'content' => isset($noteContentArr[$note->note_id]) ? $noteContentArr[$note->note_id]->content : '',
                'selected' => isset($noteSelectedArr[$note->note_id]) ? $noteSelectedArr[$note->note_id]->selected : '',
                'is_like' => isset($likeArr[$note->note_id]) && $likeArr[$note->note_id]->is_deleted == GlobalConst::IS_NOT_DELETE,
                'is_collect' => isset($collectArr[$note->note_id]) && $collectArr[$note->note_id]->is_deleted == GlobalConst::IS_NOT_DELETE,
                'user' => isset($userArr[$note->user_id]) ? ArrayHelper::merge($userArr[$note->user_id]->commonFormat(UserField::NOTE), [
                    'attention_status' => service()->RelationshipService->getAttentionStatusByRelationship($relationship, $currentUserId, $note->user_id)
                ]) : (object)[],
                'article' => isset($articleArr[$note->article_id]) ? $articleArr[$note->article_id]->commonFormat(ArticleField::NOTE) : (object)[],
                'image' => isset($imageArr[$note->note_id]) ? ArrayHelper::getColumn(Json::decode($imageArr[$note->note_id]->url), 'url') : [],
                'count' => service()->CounterService->getTargetCount(CacheKey::COUNTER_NOTE, [
                    'comment',
                    'like',
                    'collect'
                ], ['note_id' => $note->note_id]),
            ]);
        }

        return $list;
    }

    public static function formatBySearchList($noteIds)
    {
        $noteIdTable = IdBuilder::getSubTable($noteIds);

        $noteArr = self::getNoteArr($noteIdTable);

        $noteContentArr = self::getNoteContentArr($noteIdTable);

        $noteIndexArr = self::getNoteIndexeArr($noteIdTable);

        $noteSelectedArr = self::getNoteSelectedArr($noteIdTable);

        $articleArr = ArticleFormat::getArticleArr(ArrayHelper::getColumn($noteArr, 'article_id'), ArticleSelect::NOTE);

        $userArr = UserFormat::getUserArr(ArrayHelper::getColumn($noteArr, 'user_id'), UserSelect::NOTE);

        $imageArr = self::getNoteImageArr($noteIdTable);

        $list = [];
        /** @var SysNote $note */
        foreach ($noteArr as $noteId => $note) {
            $list[] = ArrayHelper::merge($note->commonFormat(NoteField::SELF_LIST), [
                'start_index' => isset($noteIndexArr[$note->note_id]) ? $noteIndexArr[$note->note_id]->start_index : 0,
                'end_index' => isset($noteIndexArr[$note->note_id]) ? $noteIndexArr[$note->note_id]->end_index : 0,
                'content' => isset($noteContentArr[$note->note_id]) ? $noteContentArr[$note->note_id]->content : '',
                'selected' => isset($noteSelectedArr[$note->note_id]) ? $noteSelectedArr[$note->note_id]->selected : '',
                'user' => isset($userArr[$note->user_id]) ? $userArr[$note->user_id]->commonFormat(UserField::NOTE) : (object)[],
                'article' => isset($articleArr[$note->article_id]) ? $articleArr[$note->article_id]->commonFormat(ArticleField::NOTE) : (object)[],
                'image' => isset($imageArr[$note->note_id]) ? ArrayHelper::getColumn(Json::decode($imageArr[$note->note_id]->url), 'url') : [],
                'count' => service()->CounterService->getTargetCount(CacheKey::COUNTER_NOTE, [
                    'comment',
                    'like',
                    'collect'
                ], ['note_id' => $note->note_id]),
            ]);
        }

        return $list;
    }

    public static function getNoteArr($noteIdTable)
    {
        $noteArr = [];
        foreach ($noteIdTable as $tableCount => $noteIdArr) {
            $notes = repository()->NoteRepository->getNoteByTableCount($tableCount, $noteIdArr);
            $noteArr = ArrayHelper::merge($notes, $noteArr);
        }

        return ArrayHelper::index($noteArr, 'note_id');
    }

    public static function getNoteContentArr($noteIdTable)
    {
        $noteContentArr = [];
        foreach ($noteIdTable as $tableCount => $noteIdArr) {
            $models = repository()->NoteRepository->getNoteContentByTableCount($tableCount, $noteIdArr);
            $noteContentArr = ArrayHelper::merge($models, $noteContentArr);
        }

        return ArrayHelper::index($noteContentArr, 'note_id');
    }

    public static function getNoteIndexeArr($noteIdTable)
    {
        $noteIndexArr = [];
        foreach ($noteIdTable as $tableCount => $noteIdArr) {
            $models = repository()->NoteRepository->getNoteLineByTableCount($tableCount, $noteIdArr);
            $noteIndexArr = ArrayHelper::merge($models, $noteIndexArr);
        }

        return ArrayHelper::index($noteIndexArr, 'note_id');
    }

    public static function getNoteSelectedArr($noteIdTable)
    {
        $noteSelectedArr = [];
        foreach ($noteIdTable as $tableCount => $noteIdArr) {
            $models = repository()->NoteRepository->getNoteSelectedByTableCount($tableCount, $noteIdArr);
            $noteSelectedArr = ArrayHelper::merge($models, $noteSelectedArr);
        }

        return ArrayHelper::index($noteSelectedArr, 'note_id');
    }

    public static function getNoteImageArr($noteIdTable)
    {
        $noteImgArr = [];
        foreach ($noteIdTable as $tableCount => $noteIdArr) {
            $models = repository()->NoteRepository->getNoteImgByTableCount($tableCount, $noteIdArr);
            $noteImgArr = ArrayHelper::merge($models, $noteImgArr);
        }

        return ArrayHelper::index($noteImgArr, 'note_id');
    }
}
